from django.contrib import admin

from goods import models


class GoodsCategoryAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        """admin后台新增或修改了数据时调用"""
        super().save_model(request, obj, form, change)

        from celery_tasks.html.tasks import generate_static_list_search_html
        generate_static_list_search_html()

    def delete_model(self, request, obj):
        """admin后台删除了数据时调用"""
        super().delete_model(request, obj)
        from celery_tasks.html.tasks import generate_static_list_search_html
        generate_static_list_search_html()


class GoodsChannelModelAdmin(admin.ModelAdmin):

    def save_model(self, request, obj, form, change):
        # super().save_model(request, obj, form, change)
        obj.save()
        from celery_tasks.html.tasks import generate_static_list_search_html
        # 重新生成半静态化的列表页
        generate_static_list_search_html()

    def delete_model(self, request, obj):
        # super().delete_model(request, obj)
        obj.delete()
        from celery_tasks.html.tasks import generate_static_list_search_html
        # 重新生成半静态化的列表页
        generate_static_list_search_html()


admin.site.register(models.GoodsCategory, GoodsCategoryAdmin)
admin.site.register(models.GoodsChannel, GoodsChannelModelAdmin)
admin.site.register(models.Brand)
admin.site.register(models.Goods)
admin.site.register(models.GoodsSpecification)
admin.site.register(models.SpecificationOption)
admin.site.register(models.SKU)
admin.site.register(models.SKUImage)
admin.site.register(models.SKUSpecification)
